<?php
namespace app\api\service;

use GuzzleHttp\Client;

class MarketingService{

    public function __construct(){
        $this->addAdmin = 'addAdmin';//【商户宝对外接口】新增管理员
        $this->editAdmin = 'editAdmin';//【商户宝对外接口】修改管理员
        $this->addSubMch = 'addSubMch';//新增门店
        $this->editSubMch = 'editSubMch';//修改门店
        $this->permission = "permission";  //【商户宝对外接口】权限列表
        $this->role = "role";//【商户宝对外接口】角色列表
    }
    public function getRole(){
        $result = $this->RequestMerchant($this->role,[]);
        return $result;
    }

    public function permission(){
        $result = $this->RequestMerchant($this->permission,[]);
        return $result;
    }

    public function addSubMch($data=[]){
        $result = $this->RequestMerchant($this->addSubMch,$data);
        return $result;
    }
    public function editSubMch($data){
        $result = $this->RequestMerchant($this->editSubMch,$data);
        return $result;
    }
    public function addAdmin($data){
        $result = $this->RequestMerchant($this->addAdmin,$data);
        return $result;
    }
    public function editAdmin($data){
        $result = $this->RequestMerchant($this->editAdmin,$data);
        return $result;
    }

    public function RequestMerchant($url,$param_data=[])
    {
        $request_data =[];
        $client = new Client();
        $request_url = "http://paytool.kelimx.com/sub_mch_admin/open/$url";
        try {
            $request_data['json'] = $param_data;
            $response = $client->post($request_url,$request_data);
            $reqs = json_decode($response->getBody()->getContents(),true);
            if (isset($reqs['status']) && $reqs['status'] == 1){
                return $reqs['result']??[];
            }
            return $reqs['message'];
        }catch (\GuzzleHttp\Exception\ClientException $exception) {
//            throw new \Exception("请求失败请联系开发人员");
            return $exception->getMessage();
        }
    }
}